package top.aigoo.project01.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import top.aigoo.project01.entity.PersonInfo;
import top.aigoo.project01.entity.WechatAuth;
import top.aigoo.project01.entity.dto.WechatAuthExecution;
import top.aigoo.project01.enums.WechatAuthStateEnum;
import top.aigoo.project01.mapper.PersonInfoMapper;
import top.aigoo.project01.mapper.WechatAuthMapper;
import top.aigoo.project01.service.WechatAuthService;
import top.aigoo.project01.utils.ImageUtil;
import top.aigoo.project01.utils.PathUtil;

/**
 * @Description: 请填写该类完成的功能
 * @Author: Mr.OYSHUO.欧阳大叔  mail:1642302522@qq.com
 * @Version 1.0.0
 * @Date: 2021/9/10 22:32
 */
@Service
public class WechatAuthServiceImpl implements WechatAuthService {
    private static Logger logger = LoggerFactory.getLogger(WechatAuthServiceImpl.class);
    @Autowired
    private WechatAuthMapper wechatAuthMapper;

    @Autowired
    private PersonInfoMapper personInfoMapper;

    @Override
    public WechatAuth getWechatAuthByOpenId(String openId) {
        return wechatAuthMapper.queryWechatInfoByOpenId(openId);
    }

    @Override
    @Transactional
    public WechatAuthExecution register(WechatAuth wechatAuth, CommonsMultipartFile profileImg) throws RuntimeException {
        //1.空值判断
        if (wechatAuth == null || wechatAuth.getOpenId() == null) {
            return new WechatAuthExecution(WechatAuthStateEnum.NULL_AUTH_INFO);
        }
        try {
            //2.如果微信账号里夹带用户信息并且用户Id为空，则认为该用户第一次使用平台(且通过微信登录) 则自动创建用户信息
            if (wechatAuth.getPersonInfo() != null && wechatAuth.getPersonInfo().getUserId() != null) {
                if (profileImg != null) {
                    try {
                        addProfileImg(wechatAuth, profileImg);
                    } catch (Exception e) {
                        logger.debug("addUserProfileImg error:" + e.toString());
                        throw new RuntimeException("addUserProfileImg error:" + e.getMessage());
                    }
                }
                //3.创建personInfo 用户信息
                try {
                    PersonInfo personInfo = wechatAuth.getPersonInfo();
                    personInfo.setEnableStatus(1);
                    int effectedNum = personInfoMapper.insertPersonInfo(personInfo);
                    wechatAuth.setPersonInfo(personInfo);
                    if (effectedNum <= 0) {
                        throw new RuntimeException("添加用户信息失败");
                    }
                } catch (Exception e) {
                    logger.error("insert PersonInfo error:" + e.toString());
                    throw new RuntimeException("insert PersonInfo error: " + e.getMessage());
                }
            }
            //4.创建专属本平台的微信账户
            int effectedNum = wechatAuthMapper.insertWechatAuth(wechatAuth);
            if (effectedNum <= 0) {
                throw new RuntimeException("帐号创建失败");
            } else {
                return new WechatAuthExecution(WechatAuthStateEnum.SUCCESS, wechatAuth);
            }
        } catch (Exception e) {
            logger.error("insert WechatAuth error:" + e.toString());
            throw new RuntimeException("insert WechatAuth error:" + e.getMessage());
        }

    }

    /**
     * 添加照片
     *
     * @param wechatAuth
     * @param profileImg
     */
    private void addProfileImg(WechatAuth wechatAuth, CommonsMultipartFile profileImg) {
        String dest = PathUtil.getPersonInfoImagePath();
        String profileImgAddr = ImageUtil.generateThumbnail(profileImg, dest);
        wechatAuth.getPersonInfo().setProfileImg(profileImgAddr);
    }

    @Override
    @Transactional
    public WechatAuthExecution register2(WechatAuth wechatAuth) throws RuntimeException {
        //1.空值判断
        if (wechatAuth == null || wechatAuth.getOpenId() == null) {
            return new WechatAuthExecution(WechatAuthStateEnum.NULL_AUTH_INFO);
        }
        try {
            //2.如果微信账号里夹带用户信息并且用户Id为空，则认为该用户第一次使用平台(且通过微信登录) 则自动创建用户信息
            if (wechatAuth.getPersonInfo() != null && wechatAuth.getPersonInfo().getUserId() == null) {

                //3.创建personInfo 用户信息
                try {
                    PersonInfo personInfo = wechatAuth.getPersonInfo();
                    personInfo.setEnableStatus(1);
                    int effectedNum = personInfoMapper.insertPersonInfo(personInfo);
                    wechatAuth.setPersonInfo(personInfo);
                    if (effectedNum <= 0) {
                        throw new RuntimeException("添加用户信息失败");
                    }
                } catch (Exception e) {
                    logger.error("insert PersonInfo error:" + e.toString());
                    throw new RuntimeException("insert PersonInfo error: " + e.getMessage());
                }
            }
            //4.创建专属本平台的微信账户
            int effectedNum = wechatAuthMapper.insertWechatAuth(wechatAuth);
            if (effectedNum <= 0) {
                throw new RuntimeException("帐号创建失败");
            } else {
                return new WechatAuthExecution(WechatAuthStateEnum.SUCCESS, wechatAuth);
            }
        } catch (Exception e) {
            logger.error("insert WechatAuth error:" + e.toString());
            throw new RuntimeException("insert WechatAuth error:" + e.getMessage());
        }

    }
}
